package com.example.administrator.readbook.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.readbook.R;
import com.example.administrator.readbook.bean.BookChapterBean;
import com.example.administrator.readbook.http.ServiceFactory;
import com.example.administrator.readbook.http.ServiceUrl;
import com.example.administrator.readbook.ui.fragment.ReadBookDialogFragment;
import com.example.administrator.readbook.utils.DisplayUtils;

import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.example.administrator.readbook.http.ServiceFactory.schedulersTransformer;

public class ChapterDetailActivity extends FragmentActivity {
    private RelativeLayout chapter_detail_layout;
    private TextView chapterDetailTv;
    private TextView chapterTitleTv;
    private String bookLink;
    private ReadBookDialogFragment dialogFragment;
    private boolean dialogIsShow = false;
    private int xLeft, xRight, yTop, yBottom;
    private float downPoint = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        ChangeThemeUtil.changeTheme(this);
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(this).inflate(R.layout.activity_chapter_detail, null);
        setContentView(view);
        xLeft = DisplayUtils.getScreenWidth(this) / 4;
        xRight = DisplayUtils.getScreenWidth(this) / 4 * 3;
        yTop = DisplayUtils.getScreenHeight(this) / 4;
        yBottom = DisplayUtils.getScreenHeight(this) / 4 * 3;
        chapter_detail_layout = findViewById(R.id.chapter_detail_layout);
        chapterDetailTv = findViewById(R.id.chapter_detail_tv);
        chapterTitleTv = findViewById(R.id.chapter_detail_title);
        bookLink = getIntent().getStringExtra("bookLink");
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downPoint = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        if (downPoint - event.getY() > 10 || event.getY() - downPoint > 10) {
                            downPoint = event.getY();
                            return false;
                        } else {
                            if (event.getX() > xLeft && event.getX() < xRight && event.getY() > yTop && event.getY() < yBottom) {
                                if (dialogIsShow) {
                                    if (dialogFragment != null) {
                                        dialogFragment.dismiss();
                                    }
                                    dialogIsShow = false;
                                } else {
                                    if (dialogFragment != null) {
                                        dialogFragment.show(getSupportFragmentManager(), "ChapterDetailActivity");
                                    }
                                    dialogIsShow = true;
                                }
                            }
                        }
                        break;
                }
                return false;
            }
        });
        getChapterDetailData();
    }

    public void getChapterDetailData() {
        ServiceUrl searchService = ServiceFactory.getServiceChapter();
        HashMap<String, String> params = new HashMap<>();
        params.put("k", "2124b73d7e2e1945");
        params.put("t", "1468223717");
        searchService.getBookChapter(bookLink, params).compose(schedulersTransformer).subscribe(new Observer<BookChapterBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BookChapterBean bookChapterBean) {
                chapterTitleTv.setText(bookChapterBean.getChapter().getTitle());
                chapterDetailTv.setText(bookChapterBean.getChapter().getBody());
                dialogFragment = ReadBookDialogFragment.getInstance(bookChapterBean.getChapter().getTitle(), new ReadBookDialogFragment.readDialogInterface() {
                    @Override
                    public void changeModel(boolean isNight) {
                        if (isNight) {
                            chapter_detail_layout.setBackgroundResource(R.color.white);
                            chapterDetailTv.setTextColor(getResources().getColor(R.color.black));
                        } else {
                            chapter_detail_layout.setBackgroundResource(R.color.black);
                            chapterDetailTv.setTextColor(getResources().getColor(R.color.text_666));
                        }
                        /*if (ChangeThemeUtil.isChange) {
                            chapter_detail_layout.setBackgroundResource(R.color.black);
                            chapterDetailTv.setTextColor(getResources().getColor(R.color.text_666));
                          *//*  if (dialogFragment != null) {
                                dialogFragment.setNightModelImg(R.mipmap.sun);
                            }*//*
                            ChangeThemeUtil.isChange = false;
                        } else {
                            chapter_detail_layout.setBackgroundResource(R.color.white);
                            chapterDetailTv.setTextColor(getResources().getColor(R.color.black));
                           *//* if (dialogFragment != null) {
                                dialogFragment.setNightModelImg(R.mipmap.moon);
                            }*//*
                            ChangeThemeUtil.isChange = true;
                        }*/
//                        ChapterDetailActivity.this.recreate();
                        dialogIsShow = false;
                        dialogFragment.dismiss();
                    }

                    @Override
                    public void dialogDismiss() {
                        dialogIsShow = false;
                    }
                }, xLeft, xRight, yTop, yBottom);
                String fileName = bookChapterBean.getChapter().getTitle();
//                String filePath =
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
